//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: '2020加油鸭！',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        imgUrls: [
            "https://desk-fd.zol-img.com.cn/t_s960x600c5/g5/M00/09/03/ChMkJl4S0EuIP7-wACjC5sva8X4AAwKEgMjv_MAKML-875.jpg",
            "https://desk-fd.zol-img.com.cn/t_s960x600c5/g5/M00/09/03/ChMkJl4S0AmIYipzACp9aDgsQ0EAAwKDQJQ3_sAKn2A391.jpg",
            "https://desk-fd.zol-img.com.cn/t_s960x600c5/g5/M00/09/03/ChMkJ14S0CWIWpCPACopZ6mH9OUAAwKEADq8FYAKil_811.jpg"
        ],
        autoplay:true,
        interval:1000,
        circular:true,
        duration: 600,
        currentSwiper:0,
        easing_function:"easeInOutCubic",
    },
    //滑动事件监听
    swiperChange: function (event) {
        this.setData({
            currentSwiper: event.detail.current
        });
    },
    //banner点击事件
    tapBanner: function (event) {
        //下面介绍两种banner 点击获取数据事件
        // 1. 通过数据源获取
        var indexUrl = this.data.imgUrls[this.data.currentSwiper];
        console.log(indexUrl);
        // 2 . 也可以通过自定义属性获取
        var url = event.currentTarget.dataset.indexurl;
        console.log(url);
    },
    //事件处理函数
    clickme: function () {
        this.setData({
            motto: "You find me!"
        });
    },
    bindViewTap: function () {
        console.log("已点击")
        wx.switchTab({
            url: '../logs/logs'
        })
    },
    onLoad: function () {
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
    getUserInfo: function (e) {
        console.log(e.detail.userInfo)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
})